import os

def modify_lflags_in_makefile(folder_path, new_lflags):
    # 遍历所有文件夹及子文件夹
    for root, dirs, files in os.walk(folder_path):
        # 遍历所有文件
        for file in files:
            # 检查是否为Makefile文件
            if file == 'Makefile':
                # 修改LFLAGS变量的值
                makefile_path = os.path.join(root, file)
                with open(makefile_path, 'r') as f:
                    lines = f.readlines()
                with open(makefile_path, 'w') as f:
                    for line in lines:
                        if line.startswith('LFLAGS='):
                            f.write(f'LFLAGS={new_lflags}\n')
                        else:
                            f.write(line)

        # 遍历所有子文件夹
        for dir in dirs:
            dir_path = os.path.join(root, dir)
            # 递归遍历子文件夹
            modify_lflags_in_makefile(dir_path, new_lflags)

# 指定文件夹路径和新的LFLAGS值
folder_path = '~/Dateset/C/testcases'
new_lflags = '-pthread -lpthread -lm'

# 调用函数
modify_lflags_in_makefile(folder_path, new_lflags)
